package QQCharting;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class Sender implements Runnable {

    int targetPort;
    DatagramSocket client = null;
    DatagramPacket packet = null;

    public Sender(int targetPort, DatagramSocket client) {
        this.targetPort = targetPort;
        this.client = client;
    }

    @Override
    public void run() {
        Scanner input = new Scanner(System.in);

        try {
            while (true){
                // 控制台发送信息
                String context = input.nextLine();
                byte[] buf = context.getBytes();
                // 将信息封装为数据报对象，指定目标IP及端口
                packet =
                 new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.255"), targetPort);
                // 发送信息
                client.send(packet);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
